package com.etlmaster.executor.steps.sql;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.StepState;
import com.etlmaster.jdbc.SqlUpdate;

/**
 * @author Wugf
 * @date 2016-03-08
 */
@StepInstruction("dop-file-sqlnolog")
public class SqlTransactionStep extends Step {

	private SqlTransactionStepMeta meta;
	
	public SqlTransactionStep(StepMeta meta) {
		super(meta);
		this.meta = (SqlTransactionStepMeta) meta;

	}

	@Override
	protected int handle() {
		List<String> sqlList = new ArrayList<String>();
		String[] sqls = meta.getSqls().split(";");
		for (String sql : sqls) {
			if(sql.trim().length()!=0)
			sqlList.add(sql);
			meta.log("INFO","执行sql:{},数据源为：{}",sql,meta.getDsName());
		}
		SqlUpdate sqlUpdate = new SqlUpdate();
		
			Map<String, Object> map = (Map<String, Object>) sqlUpdate
					.batchExecute(meta.getDsName(), sqlList);

			meta.log("INFO","{}",map.get("msg").toString());
			if((Boolean) map.get("success"))
				return StepState.RUNNING_SUCCESSED;
			else
				return StepState.RUNNING_FAILED;
		

	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
